//
//  VisitorView.swift
//  WeiboSwift
//
//  Created by xiaokui wu on 9/9/16.
//  Copyright © 2016 wxk. All rights reserved.
//

import UIKit

//protocol VisitorViewDelegate : NSObjectProtocol {
//    // 默认情况下，协议里的方法是必须实现的
//    func visitorViewDidClickLoginButton(visitor: VisitorView)
//    func visitorViewDidClickRegisterButton(visitor: VisitorView)
//}

class VisitorView: UIView {

    @IBOutlet weak var iconImageView: UIImageView!
//    @IBAction func loginBtnClick(sender: UIButton) {
//        
//        // 不用判断代理能否响应代理方法了
//        delegate?.visitorViewDidClickLoginButton(self)
//    }
//    @IBAction func registerBtnClick(sender: UIButton) {
//        delegate?.visitorViewDidClickRegisterButton(self)
//    }

    @IBOutlet weak var rotationImageView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    
    
    @IBOutlet weak var loginButton: UIButton!
    @IBOutlet weak var registerButton: UIButton!
    
    // delegate 必须用weak
//    weak var delegate: VisitorViewDelegate?
    
    
    func setupVisitorInfo(imageName: String?, title: String) {
        
        titleLabel.text = title
        guard let name = imageName else
        {
            // imageName是nil的话，就是首页,xib默认图片就是首页图片
            startRotate()
            return
        }
        // 不是首页
        iconImageView.image = UIImage(named: name)
        rotationImageView.hidden = true
    }
    
    class func visitorView() -> VisitorView {
        return NSBundle.mainBundle().loadNibNamed("VisitorView", owner: nil, options: nil).last as! VisitorView
    }
    
    private func startRotate() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.duration = 5.0
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false //
        rotationImageView.layer.addAnimation(anim, forKey: nil)
    }

}
    